<?php
require_once 'common.php';
error_reporting(0);

// 接受参数
$game 		= MooForm::request('game');		 // 必选
$channel 	= MooForm::request('channel');	 // 可选
$starttime 	= MooForm::request('starttime'); // 必选
$endtime 	= MooForm::request('endtime'); 	 // 必选
$type 		= MooForm::request('type'); 	 // 必选 select | redo

// request数据库
$requestDao = MooDao::get('Request');

if($channel == "jh") {
	$channel = "juhai";
}

if($channel) {
	$sql = "select * from @TABLE where game =:game and channel =:channel and state =:state and createTime >:starttime and createTime <:endtime";
	$arr = array(
		'game' => $game,
		'channel' => $channel,
		'state' => 0,
		'starttime' => $starttime,
		'endtime' => $endtime,
	);
} else {
	$sql = "select * from @TABLE where game =:game and state =:state and createTime >:starttime and createTime <:endtime";
	$arr = array(
		'game' => $game,
		'state' => 0,
		'starttime' => $starttime,
		'endtime' => $endtime,
	);
}

$redoData = $requestDao->getAll($sql, $arr);

// redo url
$redoConf = MooConfig::get('redo');
$urlHead  = $redoConf['urlhead'];

if($redoData) {
	foreach ($redoData as $key => $val) {
		$game = $val['game'];
		$channel = $val['channel'];
		$request = $val['request'];
		$pfOrder = $val['pfOrder'];
		
		if($channel == "uc"){
			// uc 特殊处理
			$requestArr['data'] = $request;
		} else {
			// request参数重新回调
			$requestArr = MooJson::decode($request, true);
		}
		
		$requestArr['action'] = "redo";
		
		if($type == "redo") {
			// 百度替换
			if($channel == "bddk") {
				$channel = "baidu";
			}
			
			$rechargeUrl = $urlHead . $game . "/" . $channel . "/index.php";
			
			$res = MooUtil::curl_send($rechargeUrl, $requestArr);
			$redoData[$key]['result'] = $res;
		} else if($type == "select") {
			$redoData[$key]['result'] = "fail";
		}
		
		// 防止返回数据过大 删掉json窜
		unset($redoData[$key]['request']);
 	}
}

$redoJsonData = MooJson::encode($redoData);

exit($redoJsonData);


